<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Validate;

class GoodsHistory extends Model
{
	use \base\model\ModelTrait;
	//模型关联，一对一关联
	public function profile(){
        return $this->hasOne('goods','id','goods_id')->bind([
            'goods_name'=>'name',
            'goods_no'=>'goods_no',
            'goods_img'=>'img',
            'goods_banners'=>'banners',
            'goods_info'=>'info',
            'goods_price'=>'price',
            'goods_sale_price'=>'sale_price',
            'goods_postage'=>'postage',
            'goods_unit'=>'unit',
            'goods_sales'=>'sales',
            'goods_ficti'=>'ficti',
            'is_customized',
        ]);
    }
	/**
     * 错误信息
     */
	public $errorInfo;
	/**
     * 验证规则
     */
	protected $rule = [
		'uid'=>'require|integer',
		'shop_id'=>'require|integer'
	];
	
	 /**添加
     * @return int|string
     */
    public  function addHistory($uid,$shop_id,$goods_ids='')
    {
    	$data['uid'] = $uid;
    	$data['shop_id'] = $shop_id;
    	$data['goods_id'] = $goods_ids;
      	$validate = new Validate;
		$validate->rule( $this->rule );
        if( !$validate->check($data) ) {
        	$this -> errorInfo = $validate->getError();
            return false;
        }
		if($history = self::where($data)->find()){
			$history->see_num = $history->see_num + 1;
			$history->see_time = date("Y-m-d H:i:s");
    		$history ->save();
		}else{
			$data['see_time'] = date("Y-m-d H:i:s");
    		self::allowField(true) -> save($data);
		}
    }
	
}
